function organizeData(initData = ''){
  return new Promise((resolve, reject) => {
  initData.replace(/export default \{([\s\S]*?)\}(\s+|\n?)666666666666666666/g, (_, data) => {
    data = `{${data}}`
    data = data.replace(/mixins: \[([\s\S]*?)\],/g, (_) => {
      return ''
    });
    data = data.replace(/components: \{([\s\S]*?)\},/g, (_) => {
      return ''
    });
    data = data.replace(/directives: \{([\s\S]*?)\},/g, (_) => {
      return ''
    });
    data = data.replace(/\$t\('([\s\S]*?)'\)/g, (_) => {
      return `"${_}"`
    });
    function tsOrganization(res = {}) {
      const obj = {}
      let content = ''
      let organization = []
      for (const key in res) {
        const item = res[`${key}`]
        const type = typeof item
        organization.push(key)
        if (item instanceof Array) {
          if (item.length === 0) {
            obj[`${key}`] = 'any[];'
          } else {
            const itemRow = item[0]
            let rowName = `${[`${key}Item`]}`
            rowName = rowName.charAt(0).toUpperCase() + rowName.slice(1)
            const row = tsOrganization(itemRow)
            content += `
    export interface ${rowName} ${row.data};
            `
            content+= row.content
            obj[`${key}`] = `${rowName}[]`
          }
        } else if (type !== 'object') {
          obj[`${key}`] = `${type};`
        } else {
          const row =  tsOrganization(item)
          let rowName = (key)
          rowName = rowName.charAt(0).toUpperCase() + rowName.slice(1)
          obj[`${key}`] = `${rowName};`
          content += `
    export interface ${rowName} ${row.data}
          `
        }
      }
      return {
        data: JSON.stringify(obj).replace(/":/g, '?:').replace(/"/g, ' ').replace(/,/g, '\n '),
        content,
        organization
      }
    }
    function propsOrganization (params) {
      const propsOptions = {};
      let propsValues = '';
      for (const key in params) {
        if (params.hasOwnProperty(key)) {
          const prop = params[key];
          propsOptions[key] = prop.default();
          const content = prop.default();
          if (content instanceof Array) {
            propsValues += `${key}: () => ${JSON.stringify(content)}, \n`
          } else {
            propsValues += `${key}:${ typeof content === 'string' ? `'${content}'`: content}, \n`
          }
        }
      }
      propsValues = propsValues.replace(/'\$t\('([\s\S]*?)'\)'/g, (_, a) => {
        return `$t('${a}')`
      });
      const res = tsOrganization(propsOptions)
      const propsCode = `
      const props = withDefaults(defineProps<Props>(), {
        ${propsValues}
      })
        export interface Props ${res.data}
        ${res.content}
      `;
      return {
        code: propsCode,
        list: res.organization
      }
    }
    function dataOrganization (params) {
      let values = ''
      if (params.tableColumns) {
        let tableColumnsList = []
        for (let i = 0; i < params.tableColumns.length; i++) {
          const tableColumnsItem = params.tableColumns[i]
          if (!tableColumnsItem.type || tableColumnsItem.type !== 'selection') {
            tableColumnsList.push(JSON.parse(JSON.stringify(tableColumnsItem).replace(/customSlot/g, 'slotName').replace(/headerSlot/g, 'headerSlotName')))
          }
        }
        params.tableColumns = tableColumnsList
      }
      const res = tsOrganization(params)

      for (const paramsKey in params) {
        let content = ''
        res.data.replace(new RegExp(`${paramsKey}\\?:(.*)`, 'g'), (_,v, a) => {
          content = v.replace('; ', '')
          return _
        });
        res.data.replace(new RegExp(`${paramsKey}\\?:(.*)}`, 'g'), (_,v, a) => {
          content = v.replace('; ', '')
          return _
        });
        content = content.replace(' ', '')
        values += `
         let ${paramsKey} = ref<${content}>(${JSON.stringify(params[`${paramsKey}`]).replace(/"/g, '\'')})`
      }
      const dataCode =  `
    export interface From ${res.data}
        ${res.content}
      `;
      values = values.replace(/'\$t\('([\s\S]*?)'\)'/g, (_, a) => {
        return `$t('${a}')`
      });
      return {
        code: dataCode,
        values: values,
        list: res.organization
      }
    }
    let objProps = getProps(data)
    let objData = getData(data)
    function getProps (content) {
      content = content.replace(/type: (\w+),/g, 'type: "$1",');
      const obj = eval("(" + content + ")");
      return propsOrganization(obj.props)
    }
    function getData (content) {
      const obj = eval("(" + content + ")");
      return dataOrganization(obj.data())
    }
    resolve({
      props: objProps,
      data: objData,
    })
    });
  })
}

const database = {
  organizeData
}

module.exports = database
